﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 根据类别获取活动列表
    /// </summary>
    class GetActivityListByCategoryAction : GetAction<Params.GetActivityListByCategoryParams,
        Results.GetActivityListByCategoryResult>
    {
        protected override ActionResult<Results.GetActivityListByCategoryResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            Params.GetActivityListByCategoryParams opParams)
        {
            int limit = 10;
            int start = opParams.Page * limit;

            JizSport.Common.Logger.DebugToTag(LogTag.TAG, "GetActivityListByCategoryAction get list category:{0} page:{1}", opParams.CategoryCode, opParams.Page);
            ServiceResult<PagedList<Activity>> serviceResult = null;

            if (string.IsNullOrEmpty(opParams.CategoryCode))
            {
                serviceResult = new ActivityService().SearchActivityList(opParams.Filter,
                      opParams.City,
                      opParams.Longitude,
                      opParams.Latitude,
                      opParams.TimeCondition,
                      opParams.DistanceCondition,
                      opParams.PersonCountCondition,
                      start,
                      limit);
            }
            else
            {
                serviceResult = new ActivityService().SearchActivityListByCategory(opParams.Filter,
                      opParams.CategoryCode,
                      opParams.City,
                      opParams.Longitude,
                      opParams.Latitude,
                      opParams.TimeCondition,
                      opParams.DistanceCondition,
                      opParams.PersonCountCondition,
                      start,
                      limit);
            }

            Results.GetActivityListByCategoryResult actionResult = new Results.GetActivityListByCategoryResult();

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                actionResult.Start = serviceResult.Data.Start;
                actionResult.Limit = serviceResult.Data.Limit;
                actionResult.Page = opParams.Page;
                actionResult.Total = serviceResult.Data.Total;

                for (int index = 0; index < serviceResult.Data.Items.Count; index++)
                {
                    Activity activity = serviceResult.Data.Items[index];

                    Results.GetActivityListByCategoryResult.ActivityItem item = new Results.GetActivityListByCategoryResult.ActivityItem();

                    item.Id = activity.Id;
                    item.Title = activity.Title;
                    item.CategoryCode = activity.CategoryCode;
                    item.Longitude = activity.Longitude;
                    item.Latitude = activity.Latitude;
                    item.MaxPersonCount = activity.MaxPersonCount;
                    item.CurrentPersonCount = activity.CurrentPersonCount;
                    item.BeginTime = activity.BeginTime;
                    item.EndTime = activity.EndTime;
                    item.CreatedTime = activity.CreatedTime;
                    item.IsFinished = activity.IsFinished;
                    item.Address = activity.Address;
                    item.Distance = Util.GetDistance(opParams.Latitude, opParams.Longitude, item.Latitude, item.Longitude);

                    actionResult.Items.Add(item);
                }
                if (actionResult.Start + actionResult.Items.Count >= actionResult.Total)
                {
                    actionResult.IsTheEnd = true;
                }
                else
                {
                    actionResult.IsTheEnd = false;
                }

                return new ActionResult<Results.GetActivityListByCategoryResult>("1.0.4", true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<Results.GetActivityListByCategoryResult>("1.0.4", false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }
}
